﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using pTools.cMethods;
using Newtonsoft.Json;
using pTools.PrintTable;

namespace pTools.TuQiangPrint
{
    public partial class PrintBox : DevExpress.XtraEditors.XtraForm
    {
        HttpAgreement http = new HttpAgreement();
        string methodName = "SaveBoxBarCode";

        public PrintBox()
        {
            InitializeComponent();
        }

        private void PrintBox_Load(object sender, EventArgs e)
        {
            // 
            LoadPrinterInfo();
        }

        // 加载打印机设备所需的相关数据
        void LoadPrinterInfo()
        {
            // 加载模板
            CommonMethods.loadPrintTemplte(cb_0_Template);
            // 加载打印机
            CommonMethods.loadPrintDevice(cb_0_Print);
        }

        private void btn_startPrint_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tb_barCodeNum.Text))
            {
                XtraMessageBox.Show("请填写箱内条码数量");
                return;
            }

            // 组织数据
            var boxCode = new DbProductBox
            {
                capacity = tb_barCodeNum.Text,
                barStatus = "need-in",
                isFull = "0",
                

                //isInitial = "N",
                printNum = tb_printNum.Text,
                createuser = CommonMethods.userCode
            };

            // 传入服务器接口
            var json = JsonConvert.SerializeObject(boxCode);
            var jsonResult = http.PostJson(methodName, json);

            var rsl = JsonConvert.DeserializeObject<ReturnResult>(jsonResult);

            if (rsl.Result)
            {
                var boxCodes = (string)rsl.Data;

                var dt_forPrint = new DataTable_ForPrint();
                dt_forPrint.Datas = new List<KVP_Container>();

                dt_forPrint.Columns = new List<string>
                {
                "boxCode","capacity","createdate",
                //"","",
                //"","","","","",
                };

                foreach (var code in boxCodes.Split(','))
                {
                    var kvp_boxCode = new KVP
                    {
                        Key = "boxCode",
                        Value = code
                    };

                    var kvp_capacity = new KVP
                    {
                        Key = "capacity",
                        Value = tb_barCodeNum.Text
                    };

                    var kvp_createdate = new KVP
                    {
                        Key = "createdate",
                        Value = DateTime.Now.ToString("yyyy-MM-dd")
                    };

                    var container = new KVP_Container();
                    container.kvps = new List<KVP>();
                    container.kvps.Add(kvp_boxCode);
                    container.kvps.Add(kvp_capacity);
                    container.kvps.Add(kvp_createdate);

                    dt_forPrint.Datas.Add(container);
                }

                var printDt = dt_forPrint.GetPrintTable();

                var message = "";
                CommonMethods.DoPrint(printDt, cb_0_Template.Text, cb_0_Print.Text, ref message);
            }
            else
            {
                XtraMessageBox.Show(rsl.Message);
            }
        }
    }
}